#include <REGX52.H>
#include "Delay.h"
#include "MatrixKey.h"

unsigned char MatrixKey_Num()
{
    P2 = 0xFF;
    P2_3 = 0;
    if (P2_4 == 0)
    {
        Delay(5);
        while (P2_4 == 0);
        Delay(5);
        return 1;
    }
    if (P2_5 == 0)
    {
        Delay(5);
        while (P2_5 == 0);
        Delay(5);
        return 2;
    }
    if (P2_6 == 0)
    {
        Delay(5);
        while (P2_6 == 0);
        Delay(5);
        return 3;
    }
    if (P2_7 == 0)
    {
        Delay(5);
        while (P2_7 == 0);
        Delay(5);
        return 10;
    }

    P2 = 0xFF;
    P2_2 = 0;
    if (P2_4 == 0)
    {
        Delay(5);
        while (P2_4 == 0);
        Delay(5);
        return 4;
    }
    if (P2_5 == 0)
    {
        Delay(5);
        while (P2_5 == 0);
        Delay(5);
        return 5;
    }
    if (P2_6 == 0)
    {
        Delay(5);
        while (P2_6 == 0);
        Delay(5);
        return 6;
    }
    if (P2_7 == 0)
    {
        Delay(5);
        while (P2_7 == 0);
        Delay(5);
        return 11;
    }

    P2 = 0xFF;
    P2_1 = 0;
    if (P2_4 == 0)
    {
        Delay(5);
        while (P2_4 == 0);
        Delay(5);
        return 7;
    }
    if (P2_5 == 0)
    {
        Delay(5);
        while (P2_5 == 0);
        Delay(5);
        return 8;
    }
    if (P2_6 == 0)
    {
        Delay(5);
        while (P2_6 == 0);
        Delay(5);
        return 9;
    }
    if (P2_7 == 0)
    {
        Delay(5);
        while (P2_7 == 0);
        Delay(5);
        return 12;
    }

    P2 = 0xFF;
    P2_0 = 0;
    if (P2_4 == 0)
    {
        Delay(5);
        while (P2_4 == 0);
        Delay(5);
        return 0;
    }
    if (P2_5 == 0)
    {
        Delay(5);
        while (P2_5 == 0);
        Delay(5);
        return 13;
    }
    if (P2_6 == 0)
    {
        Delay(5);
        while (P2_6 == 0);
        Delay(5);
        return 14;
    }
    if (P2_7 == 0)
    {
        Delay(5);
        while (P2_7 == 0);
        Delay(5);
        return 15;
    }
    return 0;
}
